layui.use(['form','treeSelect','layer','upload','laydate','layedit','tableSelect'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;
    var upload = layui.upload;
    var laydate = layui.laydate;
    var treeSelect= layui.treeSelect;//树形下拉框
    var imgURL="";//多图片上传的路径
    var facilityDesc;

    let tempStatus = null;
    //回显数据
    var request = GetRequest();
    $.get("/admin/facility/getFacility?facilityId=" + request.id,function (data) {
        $("#categoryId").val(data.categoryId);
        $.ajax({
            //根据parentId查对应类别，从而拿到父类结点的名称
            url: '/admin/facilityCategory/getFacilityCategory?facilityCategoryId='+data.categoryId,
            type: 'GET',
            sync:true,
            success: function (da) {
                if(da!=null) {
                    //渲染菜单的数据到表单元素
                    treeSelect.render({
                        elem: '#categoryName',
                        data: '/admin/facilityCategory/treelist',
                        type: 'get',
                        placeholder: da.name,// 占位符
                        search: true,// 是否开启搜索功能：true/false，默认false
                        click: function(d){
                            $("#categoryName").val(d.current.name);
                            $("#categoryId").val(d.current.id);
                        },
                        success: function () { // 加载完成后的回调函数
                            $("#categoryName").val(da.name);

                        }
                    });
                }
            }})
        $("#facilityId").val(data.facilityId);
        $("#facilityName").val(data.facilityName);
        $("#storageId").val(data.storageId);
        $.ajax({
            //根据parentId查对应类别，从而拿到父类结点的名称
            url: '/admin/storage/getStorage?StorageId='+data.storageId,
            type: 'GET',
            sync:true,
            success: function (da) {
                if(da!=null) {
                    //渲染菜单的数据到表单元素
                    treeSelect.render({
                        elem: '#storageName',
                        data: '/admin/storage/treelist',
                        type: 'get',
                        placeholder: da.name,// 占位符
                        search: true,// 是否开启搜索功能：true/false，默认false
                        click: function(d){
                            $("#storageId").val(d.current.storageId);
                            $("#storageName").val(d.current.name);
                        },
                        success: function () { // 加载完成后的回调函数
                            $("#storageName").val(da.name);
                        }
                    });

                }
            }})
        $("#manufacturer").val(data.manufacturer);
        $("#manufactureNo").val(data.manufactureNo);
        $("#facilityNo").val(data.facilityNo);
        var date = new Date(data.buyDate);
        laydate.render({
            elem: '#buyDate'
            ,value: date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()   //必须遵循format参数设定的格式  + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds()
        });
        $("#fixedAssetsNo").val(data.fixedAssetsNo);
        $("#materialStorageId").val(data.materialStorageId);
        $.ajax({
            //根据parentId查对应类别，从而拿到父类结点的名称
            url: '/admin/fileStorage/getStorage?StorageId='+data.materialStorageId,
            type: 'GET',
            sync:true,
            success: function (da) {
                console.log(da)
                if(da!=null) {
                    //渲染菜单的数据到表单元素
                    treeSelect.render({
                        elem: '#materialStorage',
                        data: '/admin/fileStorage/treelist',
                        type: 'get',
                        placeholder: da.name,// 占位符
                        search: true,// 是否开启搜索功能：true/false，默认false
                        click: function(d){
                            $("#materialStorageId").val(d.current.storageId);
                            $("#materialStorage").val(d.current.name);
                        },
                        success: function () { // 加载完成后的回调函数
                            $("#materialStorage").val(da.name);
                        }
                    });

                }
            }})
        $("#supplierCompany").val(data.supplierCompany);
        $("#supplier").val(data.supplier);
        $("#supplierMobile").val(data.supplierMobile);
        $("#afterSaleMobile").val(data.afterSaleMobile);
        $("#warrantyPeriod").val(data.warrantyPeriod);
        $("#status").val(data.status);
        tempStatus = data.status;
        if(data.status==0)
        $("#status").prop("checked",true);


        $("#buyer").val(data.buyer);
        $("#supervisor").val(data.supervisor);

        form.render('checkbox');

    });



    //监听仪器是否可用开关
    form.on('switch(switchTest)', function(data){
        if (this.checked) {
            $("#status").val('0');
            tempStatus = "0";
            console.log(0);
        } else {
            $("#status").val('1');
            tempStatus = "1";
            console.log(1);
        }
    });

    form.on("submit(edit)",function(data){
        form.render();
        data.field.status = tempStatus;
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        $.ajax({
            url: '/admin/facility/edit',
            data: data.field,
            type: 'post',
            success: function (data) {
                if(data.code == 200) {
                    top.layer.close(index);
                    top.layer.msg("修改成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                } else {
                    console.log(11111)
                    top.layer.close(index);
                    top.layer.msg("修改失败！");
                }
            },
            error:function () {
                top.layer.close(index);
                top.layer.msg("添加失败！");
            }
        });
        return false;
    })


    //获取路径中的参数
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }


});